package resource.pojo;

import java.io.Serializable;
import java.util.Date;
import java.sql.ResultSet;
import models.util.MySqlDataAccessHelper;

public class Typedoc implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer iDTypeDoc;
	private String typeName;
	private Integer iDContentDoc;
	private Integer iDTemplate;
	private Template template;
	private Integer iDConfigStaticLang;
	private String description;
	private Date timecreate;
	private Integer views;
	private Contentdoc contentDoc;
	private ConfigStaticLang configStaticLang;
	MySqlDataAccessHelper mysqlHelper = new MySqlDataAccessHelper();

	public MySqlDataAccessHelper getMysqlHelper() {
		return mysqlHelper;
	}

	public void setMysqlHelper(MySqlDataAccessHelper mysqlHelper) {
		this.mysqlHelper = mysqlHelper;
	}

	public Typedoc(Integer iDConfigStaticLang) {
		this.iDConfigStaticLang = iDConfigStaticLang;
	}

	public Typedoc(){
		
	}
	
	public Typedoc(Integer iDTypeDoc, String typeName, Integer iDContentDoc,
			Integer iDTemplate, Integer iDConfigStaticLang, String description,Date d,Integer views) {
		super();
		this.iDTypeDoc = iDTypeDoc;
		this.typeName = typeName;
		this.iDContentDoc = iDContentDoc;
		this.iDTemplate = iDTemplate;
		this.iDConfigStaticLang = iDConfigStaticLang;
		this.description = description;
		this.timecreate = d;
		this.views = views;
	}

	public Integer getViews() {
		return views;
	}

	public void setViews(Integer views) {
		this.views = views;
	}

	public Date getTimecreate() {
		return timecreate;
	}

	public void setTimecreate(Date timecreate) {
		this.timecreate = timecreate;
	}
	
	public Integer getiDTypeDoc() {
		return iDTypeDoc;
	}

	public void setiDTypeDoc(Integer iDTypeDoc) {
		this.iDTypeDoc = iDTypeDoc;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public Integer getiDContentDoc() {
		return iDContentDoc;
	}

	public void setiDContentDoc(Integer iDContentDoc) {
		this.iDContentDoc = iDContentDoc;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getiDTemplate() {
		return iDTemplate;
	}

	public void setiDTemplate(Integer iDTemplate) {
		this.iDTemplate = iDTemplate;
	}

	public Template getTemplate() {
		mysqlHelper.open();
		try {
			String sql = "select * from template where IDTemplate="
					+ this.iDTemplate;
			ResultSet rs = mysqlHelper.executeQuery(sql);
			if (rs.next())
				template = new Template(rs.getInt("IDTemplate"),
						rs.getString("TemplateName"),rs.getInt("IDConfigStaticLang"), rs.getString("Description"));
			else
				template = new Template();
		} catch (Exception e) {
			template = new Template();
		} finally {
			mysqlHelper.close();
		}
		
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	
	public Contentdoc getContentDoc() {
		mysqlHelper.open();
		try {
			String sql = "select * from contentdoc where IDContentDoc="
					+ this.iDContentDoc;
			ResultSet rs = mysqlHelper.executeQuery(sql);
			if (rs.next())
				contentDoc = new Contentdoc(rs.getInt("IDContentDoc"),
						rs.getString("Path"), rs.getString("Description"));
			else
				contentDoc = new Contentdoc();
		} catch (Exception e) {
			contentDoc = new Contentdoc();
		} finally {
			mysqlHelper.close();
		}
		return contentDoc;
	}

	public void setContentDoc(Contentdoc contentDoc) {
		this.contentDoc = contentDoc;
	}

	public ConfigStaticLang getConfigStaticLang() {
		if (this.iDConfigStaticLang == 0)
			return configStaticLang = null;
		mysqlHelper.open();
		try {
			String sql = "select * from staticlang where IDConfigStaticLang="
					+ this.iDConfigStaticLang;
			ResultSet rs = mysqlHelper.executeQuery(sql);
			if (rs.next())
				configStaticLang = new ConfigStaticLang(
						rs.getInt("IDConfigStaticLang"), rs.getString("VN"),
						rs.getString("KR"), rs.getString("EN"));
			else
				configStaticLang = null;
		} catch (Exception e) {
		} finally {
			mysqlHelper.close();
		}
		return configStaticLang;
	}

	public void setConfigStaticLang(ConfigStaticLang configStaticLang) {
		this.configStaticLang = configStaticLang;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((iDContentDoc == null) ? 0 : iDContentDoc.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Typedoc other = (Typedoc) obj;
		if (iDContentDoc == null) {
			if (other.iDContentDoc != null)
				return false;
		} else if (!iDContentDoc.equals(other.iDContentDoc))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Typedoc [typeName=" + typeName + "]";
	}

}
